package com.ccx.coachsite.util;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.ccx.coachsite.R;

import android.app.Activity;
import android.content.ComponentName;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.content.Intent.ShortcutIconResource;
import android.database.Cursor;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.Uri;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ImageView;
/**
 * <p>Title: 常用工具类         </p>
 * <p>Description: 包括
 * 1. 判断网络是否连接正常
 * 2. 隐藏键盘
 * 3. 显示EditText错误信息
 * 4. 创建快捷方式
 * 5. 删除快捷方式
 * 6. 图片切换特效
 * </p>
 * <p>@author: Eric.wsd                </p>
 * <p>Copyright: Copyright (c) 2012    </p>
 * <p>Company: ffcs Co., Ltd.          </p>
 * <p>Create Time: 2012-6-3             </p>
 * <p>@author:                         </p> 
 * <p>Update Time:                     </p>
 * <p>Updater:                         </p>
 * <p>Update Comments:                 </p>
 */
public class CommonViewUtils {

	/**
	 * 判断网络是否可用
	 * @param context
	 * @return true可用 false 不可用
	 */
	public static boolean isNetConnectionAvailable(Context context) {
		ConnectivityManager connectivityManager = (ConnectivityManager) context
				.getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo activeNetInfo = connectivityManager.getActiveNetworkInfo();
		if (activeNetInfo != null && activeNetInfo.isAvailable()) {
			return true;
		} else {
			return false;
		}
	}

	/**
	 * 隐藏键盘
	 * 
	 * @param a
	 */
	public static void hideKeyboard(final Activity a) {
		InputMethodManager inputManager = (InputMethodManager) a.getApplicationContext().getSystemService(
				Context.INPUT_METHOD_SERVICE);
		if (inputManager != null) {
			View focusView = a.getCurrentFocus();
			if(focusView != null)
			{
				inputManager.hideSoftInputFromWindow(a.getCurrentFocus().getWindowToken(),
						InputMethodManager.HIDE_NOT_ALWAYS);
			}
			
		}
	}
	

	/**
	 * EidtText控件显示错误信息
	 * 参数以String传入
	 * @param et
	 * @param error  字符串参数
	 * @param animation
	 */
	public static void showErrorByEditText(EditText et, String error, Animation animation) {
		et.requestFocus();
		et.setError(error);
		et.startAnimation(animation);
	}
	
	/**
	 * EditText控件显示错误信息
	 * 参数以R.string.xxx传入
	 * @param et
	 * @param resId 资源ID
	 * @param animation
	 */
	public static void showErrorByEditText(EditText et, int resId, Animation animation) {
		String error = et.getResources().getString(resId);
		et.requestFocus();
		et.setError(error);
		et.startAnimation(animation);
	}

	/**
	 * 图片切换特效
	 * @param imageView
	 */
	public static void showImageChange(ImageView imageView) {
		AlphaAnimation animation = new AlphaAnimation(0.1f, 1.0f);
		animation.setDuration(1000);
		imageView.startAnimation(animation);
	}
	
	/**
	 * 正则匹配手机号码
	 * 
	 * @param mobileNo
	 * @return
	 */
	public static boolean isMobileNoValid(String mobileNo) {
		String regExp = "^[1][0-9]{10}$";

		Pattern p = Pattern.compile(regExp);

		Matcher m = p.matcher(mobileNo);

		return m.find();
		
	}
	
	/**
	 * 判断是否存在快捷方式
	 *  
	 * @param ctx
	 * @return
	 */
	public static boolean hasShortcut(Context ctx, int app_name) {
		boolean isInstallShortcut = false;
		final ContentResolver cr = ctx.getContentResolver();
		final String AUTHORITY;
		// 在andriod 2.1即SDK7以上，是读取launcher.settings中的favorites表的数据；
		// 在andriod 2.2即SDK8以上，是读取launcher2.settings中的favorites表的数据。
		if (getSystemVersion() < 8) {
			AUTHORITY = "com.android.launcher.settings";
		} else {
			AUTHORITY = "com.android.launcher2.settings";
		}

		final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY + "/favorites?notify=true");
		Cursor c = cr.query(CONTENT_URI, new String[] { "title", "iconResource" }, "title=?",
				new String[] { ctx.getString(app_name).trim() }, null);
		if (c != null && c.getCount() > 0) {
			isInstallShortcut = true;
		}
		return isInstallShortcut;
	}

	/**
	 * 添加快捷方式
	 * 
	 * @param act
	 */
	public static void createShortcut(Activity act, int app_name, int icon) {
		Intent shortcut = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");
		// 快捷方式的名称
		shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, act.getString(app_name));
		shortcut.putExtra("duplicate", false); // 不允许重复创建
		// 指定当前的Activity为快捷方式启动的对象: 如 com.everest.video.VideoPlayer
		// 注意: ComponentName的第二个参数必须加上点号(.)，否则快捷方式无法启动相应程序
		ComponentName comp = new ComponentName(act.getPackageName(), ".SplashActivity");
		shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, new Intent(Intent.ACTION_MAIN).setComponent(comp));
		// 快捷方式的图标
		ShortcutIconResource iconRes = Intent.ShortcutIconResource.fromContext(act, icon);
		shortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, iconRes);
		act.sendBroadcast(shortcut);
	}

	/**
	 * 删除快捷方式
	 * @param act
	 */
	public static void removeShortcut(Activity act, int app_name) {
		Intent shortcut = new Intent("com.android.launcher.action.UNINSTALL_SHORTCUT");
		// 快捷方式的名称
		shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, act.getString(app_name));
		// 指定当前的Activity为快捷方式启动的对象: 如 com.everest.video.VideoPlayer
		// 注意: ComponentName的第二个参数必须是完整的类名（包名+类名），否则无法删除快捷方式
		String appClass = act.getPackageName() + "." + act.getLocalClassName();
		ComponentName comp = new ComponentName(act.getPackageName(), appClass);
		shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, new Intent(Intent.ACTION_MAIN).setComponent(comp));
		act.sendBroadcast(shortcut);
	}

	/**
	 * 返回系统SDK版本号
	 * @return
	 */
	public static int getSystemVersion() {
		return android.os.Build.VERSION.SDK_INT;
	}
	
	public static void SetStarImage(int star, ImageView imgStar)
	{
		switch (star) {
		case 1:
			imgStar.setImageResource(R.drawable.soft_mgr_star_1);
			break;
		case 2:
			imgStar.setImageResource(R.drawable.soft_mgr_star_2);
			break;
		case 3:
			imgStar.setImageResource(R.drawable.soft_mgr_star_3);
			break;
		case 4:
			imgStar.setImageResource(R.drawable.soft_mgr_star_4);
			break;
		case 5:
			imgStar.setImageResource(R.drawable.soft_mgr_star_5);
			break;
		default:
			imgStar.setImageResource(R.drawable.soft_mgr_star_0);
			break;
		}
	}
}
